package com.company.test;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
//map：接收一个函数作为参数，该函数会被应用到每个元素上，并将其映射成一个新的元素。
public class MapTest2 {
  public static void main(String[] args) {
    List<Person> personList = new ArrayList<Person>();
    personList.add(new Person("Tom", 8900, 23, "male", "New York"));
    personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
    personList.add(new Person("Lily", 7800, 21, "female", "Washington"));
    personList.add(new Person("Anni", 8200, 24, "female", "New York"));
    personList.add(new Person("Owen", 9500, 25, "male", "New York"));
    personList.add(new Person("Alisa", 7900, 26, "female", "New York"));

    // 不改变原来集合的方式
    List<Person> newList =
        personList.stream()
            .map(
                person -> {
                  Person newPerson = new Person(person.getName(), 0, 0, null, null);
                  newPerson.setSalary(person.getSalary() + 1000);
                  return newPerson;
                })
            .collect(Collectors.toList());

    System.out.println(
        "一次改动前" + personList.get(0).getName() + "-------------->" + personList.get(0).getSalary());
    System.out.println(
        "一次改动后" + newList.get(0).getName() + "-------------->" + newList.get(0).getSalary());

    // 改变原来的集合
    List<Person> newList2 =
        personList.stream()
            .map(
                person -> {
                  person.setSalary(person.getSalary() + 1000);
                  return person;
                })
            .collect(Collectors.toList());
    System.out.println(
        "二次改动前" + personList.get(0).getName() + "-------------->" + personList.get(0).getSalary());
    System.out.println(
        "二次改动后" + newList2.get(0).getName() + "-------------->" + newList2.get(0).getSalary());
  }
}
